﻿/******************************************************************
** 文 件 名: Bu_BatchContract_DAL.cs
** Copyright (c) 2010-2012 Honestysoft (GuangZhou) Co.,LTD
** 创 建 人: 此代码由生成器自动生成
** 创建日期: 2012-3-21 15:53:10
** 描    述: 实现公共部分的数据访问封装，提供批次包含合同的数据访问类
** 版    本: 2.0.0.0
** 修改描述:
** 修 改 人:
** 修改日期:
******************************************************************/
using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Text;
using Honestysoft.BaseObject;
using Honestysoft.Financing.Model;

namespace Honestysoft.Financing.DataAccess
{
	/// <summary>
    /// Bu_BatchContract数据访问类
    /// </summary>
	public partial class Bu_BatchContract_DAL : BaseDataAccess<Bu_BatchContract>
    {		
		/// <summary>
        /// 构造函数，使用指定的连接字符串
        /// </summary>
        /// <param name="connectionStringName">连接字符串名称</param>
        public Bu_BatchContract_DAL(string connectionStringName)
            : base(connectionStringName)
        {

        }

        public DataTable GetBatchContract(string batchID)
        {
            DbParameter parameter = GetDbParameter("BatchID",batchID);
            string sql = string.Format(@"Select A.*,B.Name ContractName,C.Name ProjectName 
                From Bu_BatchContract A
                Inner Join BD_Contract B On A.ContractID=B.ID
                Inner Join BD_Project C On B.ProjectID=C.ID
                Where A.BatchID={0}
                Order by C.SortCode,B.Code", parameter.ParameterName);
            DbCommand command = GetDbCommand(sql);
            command.Parameters.Add(parameter);
            return ExecuteDataTable(command);
        }

        /// <summary>
        /// 删除批次回执包含的合同
        /// </summary>
        /// <param name="batchID"></param>
        /// <param name="transaction"></param>
        /// <returns></returns>
        public int DeleteBatchContract(string batchID,DbTransaction transaction)
        {
            DbParameter parameter = GetDbParameter("BatchID", batchID);
            string sql = string.Format(@"Delete From Bu_BatchContract
                Where ContractID not in
                (Select A.ContractID From Bu_DocReceive A Inner Join Bu_BatchRec B
                On A.ID=B.RecID And B.BatchID={0})
                And BatchID={0}", parameter.ParameterName);
            DbCommand command = GetDbCommand(sql);
            command.Parameters.Add(parameter);
            return ExecuteNonQuery(command, transaction);
        }

        /// <summary>
        /// 根据回执ID删除批次对应的合同
        /// </summary>
        /// <param name="arrRecID"></param>
        /// <param name="transaction"></param>
        /// <returns></returns>
        public int DeleteByRecID(string[] arrRecID, DbTransaction transaction)
        {
            string sql = @"Select * From Bu_BatchContract
                Where ContractID not in
                (Select A.ContractID From Bu_DocReceive A Inner Join Bu_BatchRec B
                On A.ID=B.RecID And A.ID in ('" + string.Join("','", arrRecID) + @"'))
                And BatchID in (Select B.BatchID From Bu_DocReceive A Inner Join Bu_BatchRec B
                On A.ID=B.RecID And A.ID in ('" + string.Join("','", arrRecID) + @"'))";
            return ExecuteNonQuery(sql, transaction);
        }
    }
}
